package cn.com.controller;

import cn.com.Authorization;
import cn.com.beans.CollegeInfo;
import cn.com.beans.QuerySuggestion;
import cn.com.beans.ScoreLine;
import cn.com.service.CollegeInfoService;
import cn.com.service.ScoreLineService;
import cn.com.util.ListOperation;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * 推荐页面控制器
 * @author 龙战广
 */
@Controller
@RequestMapping("/recommend")
public class RecommendController {
    @Resource
    private CollegeInfoService collegeInfoService;
    @Resource
    private ScoreLineService scoreLineService;

    @GetMapping
    @Authorization(url = "/recommend",authority = "r",methods = RequestMethod.GET)
    public String home(){
        return "predictionRrecommended/recommend";
    }

    @GetMapping("/inquire")
    public String inquire(HttpServletRequest request){
        Integer score = Integer.valueOf(request.getParameter("score"));
        Integer subject = Integer.valueOf(request.getParameter("subject"));
        int probability = Integer.valueOf(request.getParameter("probability"));
        String classes = request.getParameter("classes");
        Integer scoremax;
        Integer scoremin;
        if(probability==0){
            scoremax = (int)score+30;
            scoremin = (int)score+10;
        }else if(probability==1){
            scoremax = (int)score+10;
            scoremin = (int)score-10;
        }else {
            scoremax = (int)score-10;
            scoremin = (int)score-30;
        }
        QuerySuggestion querySuggestion = new QuerySuggestion();
        querySuggestion.setScoremax(scoremax);
        querySuggestion.setScoremin(scoremin);
        querySuggestion.setSubject(subject);
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        querySuggestion.setYear(year);
        CollegeInfo collegeInfo = new CollegeInfo();
        collegeInfo.setCoCategory(classes);
        List<ScoreLine> scoreLines = scoreLineService.selectRecommendCoId(querySuggestion);
        List<CollegeInfo> collegeInfos = collegeInfoService.findAll(collegeInfo);
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for(ScoreLine s : scoreLines){
            list1.add(s.getScCoid());
        }
        for(CollegeInfo c : collegeInfos){
            list2.add(c.getCoId());
        }
        List<Integer> same = ListOperation.same(list1, list2);
        int[] coids = new int[same.size()];
        for(int i = 0;i < same.size();i++){
            coids[i]=same.get(i);
        }
        System.out.println(coids);
        HttpSession session = request.getSession();
        session.setAttribute("coids",coids);
        session.setAttribute("rpage",1);
        return "redirect:/recommend/collegelist";
    }
    @GetMapping("/collegelist")
    public String College(ModelMap modelMap, Integer page, @SessionAttribute Integer[] coids) {
        if(page == null) {
            page = 1;
        }
        System.out.println(2);
        System.out.println(coids);
        List<CollegeInfo> collegelist = new ArrayList<>();
            for(Integer i:coids){
                CollegeInfo collegeInfo = collegeInfoService.selectByPrimaryKey(i);
                collegelist.add(collegeInfo);
            }
        Integer sum = collegelist.size();
        modelMap.addAttribute("collegeList", collegelist);
        modelMap.addAttribute("cpage",page);
        modelMap.addAttribute("sum",sum);
        return "predictionRrecommended/collegelist";
    }

}
